/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.raitcon.hibernate.db;

import com.raitcon.hibernate.bean.Alumno;
import com.raitcon.hibernate.bean.DetalleNota;
import com.raitcon.hibernate.bean.HibernateSessionFactory;
import com.raitcon.hibernate.bean.Nota;
import com.raitcon.hibernate.factory.HibernateUtil;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Mike
 */
public class DetalleNotaDB {
    
    protected static Logger log = Logger.getLogger(DetalleNotaDB.class);
    private Session session = null;
    
    
    public DetalleNotaDB(){
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
    }
    
    
    
    
    public String insertNota(Nota nota, List<DetalleNota> detalleNota) {
        String result = "";
         System.out.println("metodo insert  nota");
         session = HibernateSessionFactory.getSession();
         System.out.println("session: " + session);
         System.out.println("Try");
         Transaction ts = session.beginTransaction();
        try {
             
            System.out.println("Transaction");
            session.save(nota);
            
            System.out.println("Save nota");
            for(DetalleNota d :detalleNota ){
                d.setNota(nota);
                session.save(d);
                System.out.println("Save detlle");
             
            }
            ts.commit();
            System.out.println("Se inserto correctamente");
            result = "Se relizó el guardo exitosamnte el registo nota id"+nota.getIdNota();
            return result;
        } catch (HibernateException ex1) {
            System.out.println("HibernateException : " + ex1);
            ts.rollback();
            return "";
        } catch (Exception ex2) {
            System.out.println("Exception : " + ex2);
            ts.rollback();
            return "";
        } finally {
            session.close();
        }
    }
    
    
    public String save(DetalleNota dtNota, Session osession) {
        String result = "";
         System.out.println("metodo insert  nota");
         session = HibernateSessionFactory.getSession();
         System.out.println("session: " + session);
         System.out.println("Try");
         //Transaction ts = session.beginTransaction();
        try {
             
           // System.out.println("Transaction");
            
            
            session.save(dtNota);
           // ts.commit();
         //  System.out.println("Se inserto correctamente");
         //   result = "Se relizó el guardo exitosamnte el registo nota id"+nota.getIdNota();
            return result;
        } catch (HibernateException ex1) {
            System.out.println("HibernateException detalle nota : " + ex1);
            //ts.rollback();
            return "";
        } catch (Exception ex2) {
            System.out.println("Exception detalle nota: " + ex2);
           // ts.rollback();
            return "";
        }
        //    finally {
       //     session.close();
      //  }
    }
    
     public List<DetalleNota> getDetalleNotaFilter(Long idNota){
        
         log.info("Iniciando consulta de DetalleNota...");
        List<DetalleNota> cedulaMatriculaList = null;
        try {
            Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from DetalleNota as detalleNota "
                     + "WHERE id_nota="+idNota+" order by id_nota_detalle" );

            cedulaMatriculaList = (List<DetalleNota>) q.list();
             log.info("Consulta de detalle nota exitosa.");
        } catch (Exception e) {
            log.error("Error:" + e.getMessage());
            e.printStackTrace();
        } finally {
            //session.close();
        }
        return cedulaMatriculaList;
    }
    
    
    
    
}
